/**
 * sourcesite adaptor class for ISYATIRIM (ISBANK).
 */
var isteyatirim = function() {
	/* bof private area */

	/**
	 * @private function
	 * executes given regular expression on given text,
	 * and returns string within first matched parantheses
	 * in the regular expression.
	 *
	 * @param Regexp regex, regular expression to be executed.
	 * @param String text, source on which given regular expression
	 * will be executed.
	 *
	 * @return Float
	 */
	var getFirstMatch = function(regex, text) {
		var matches = regex.exec(text);
		return parseFloat(matches[1].replace(/,/g, "."));
	}

	/**
	 * @private function
	 * executes regular expression on given text to get given exchange
	 * type's values.
	 *
	 * @param String responseText, source on which regular expression
	 * will be executed.
	 * @param String exchange_type, EURO or USD
	 *
	 * @return Hash, "value" is the buy value of given exchange
	 * type. "tooltip" is in "<BUY_LABEL>: <BUY_VALUE> | <SELL_LABEL>:
	 * <SELL_VALUE>" format.
	 */
	var parseResponseForExtra = function(responseText, exchange_type) {
		var buy_span_id = "ctl00_ContentPlaceHolder1_DovizDovizTabloUC1_lbl_M_" + exchange_type + "_A";
		var sell_span_id = "ctl00_ContentPlaceHolder1_DovizDovizTabloUC1_lbl_M_" + exchange_type + "_S";
		var regex = new RegExp("^.*<span id=\"" + buy_span_id + "\">([^<]+)<\/span>.*$", 'm');
		var matches = regex.exec(responseText);
		var buyval = matches[1];
		regex = new RegExp("^.*<span id=\"" + sell_span_id + "\">([^<]+)<\/span>.*$", 'm');
		matches = regex.exec(responseText);
		var sellval = matches[1];
		return {
			value: buyval,
			tooltip: imkbizle.locale.getString("stock.Buy") + ": " + buyval
				+ " | " + imkbizle.locale.getString("stock.Sell") + ": " + sellval
		};
	};
	/* eof private area */

	return {
	/* bof public area */

		/**
		 * @public variable
		 * String, source site name. it's used in prefences dialog in "source
		 * site" menupopup.
		 */
		sitename: "ISTEYATIRIM",

		/**
		 * @public variable
		 * String, source site main url where stock information is
		 * queried. the parameters passed changes according to chosen
		 * stock. see createParams function for details.
		 */
		url: "http://www.isteyatirim.com.tr/Borsa/BorsaSirketKarti.aspx",

		/**
		 * @public variable
		 * Hash, extra statistics that can be retrieved from source
		 * site (such as exchange values). these statistics are added
		 * to the end of select buttons' list. hash items are also
		 * hash objects that must have "url" and "parse" keys. "parse"
		 * points to parser function to get wanted value from response
		 * of request to "url"
		 */
		extras: {
			EURTRY: {
				url: "http://www.isteyatirim.com.tr/Doviz/DovizDefault.aspx",
				parse: function(responseText) {
					return parseResponseForExtra(responseText, "EUR");
				}
			},
			USDTRY: {
				url: "http://www.isteyatirim.com.tr/Doviz/DovizDefault.aspx",
				parse: function(responseText) {
					return parseResponseForExtra(responseText, "USD");
				}
			}
		},

		/**
		 * @public function
		 * create request params that will be passed to Ajax.request
		 * function of prototype library.
		 *
		 * @param String stockname, for which the request url will be
		 * formed.
		 *
		 * @return Hash, {SENETADI: <STOCKNAME>.IS}
		 */
		createParams: function(stockname) {
			return {
				EnstrumanKodu: stockname
			};
		},

		/**
		 * @public function
		 * gets name of stock from parsed response text.
		 *
		 * @param String responseText, response html returned from
		 * stock page.
		 *
		 * @return String, name of stock.
		 */
		getName: function(responseText) {
			var regex = /^.*EnstrumanKodu=([^.]+)\"\ id=\"aspnetForm\".*$/m;
			var matches = regex.exec(responseText);
			return matches[1];
		},

		/**
		 * @public function
		 * gets latest value of stock from parsed response text.
		 *
		 * @param String responseText, response html returned from
		 * stock page.
		 *
		 * @return Float, stock's latest value.
		 */
		getLatest: function(responseText) {
			var regex = /^.*<span id="ctl00_ContentPlaceHolder1_lblSonIslemFiyati">([^<]+)<\/span>.*$/m;
			return getFirstMatch(regex, responseText);
		},

		/**
		 * @public function
		 * gets last closed value of stock from parsed response text.
		 *
		 * @param String responseText, response html returned from
		 * stock page.
		 *
		 * @return Float, stock's last closed value.
		 */
		getOpen: function(responseText) {
			return "N/A";
		},

		/**
		 * @public function
		 * gets change percentage of stock between latest and open
		 * value from parsed response text.
		 *
		 * @param String responseText, response html returned from
		 * stock page.
		 *
		 * @return String, "%<CHANGE_PERCENTAGE>"
		 */
		getChange: function(responseText) {
			var regex = /^.*<span id="ctl00_ContentPlaceHolder1_lblYuzdeDegisim">([^<]+)<\/span>.*$/m;
			return "%" + getFirstMatch(regex, responseText);
		},

		/**
		 * @public function
		 * gets volume of stock operations from parsed response text.
		 *
		 * @param String responseText, response html returned from
		 * stock page.
		 *
		 * @return Float, stock's volume.
		 */
		getVol: function(responseText) {
			return "N/A";
		},

		/**
		 * @public function
		 * gets buy value of stock from parsed response text.
		 *
		 * @param String responseText, response html returned from
		 * stock page.
		 *
		 * @return Float, stock's buy value.
		 */
		getBuy: function(responseText) {
			// TODO:  we need to do a second request to get buy value, because it's in another page.
			return "N/A";
		},

		/**
		 * @public function
		 * gets sell value of stock from parsed response text.
		 *
		 * @param String responseText, response html returned from
		 * stock page.
		 *
		 * @return Float, stock's sell value.
		 */
		getSell: function(responseText) {
			// TODO:  we need to do a second request to get sell value, because it's in another page.
			return "N/A";
		}

	/* eof public area */
	}
}();
//extend sourcesite and overwrite duplicated properties
var sourcesite_clone = imkbizle_jquery.extend({}, sourcesite);
isteyatirim = imkbizle_jquery.extend(sourcesite_clone, isteyatirim);

